package cn.promonkey.api.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.promonkey.core.dto.UserDto;
import cn.promonkey.core.response.ResultMsg;
import cn.promonkey.core.toolkit.QueryGenerator;
import cn.promonkey.core.toolkit.UserInfoUtils;
import jakarta.servlet.http.HttpServletRequest;
import cn.promonkey.api.contract.entity.Contract;
import cn.promonkey.api.contract.service.ContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;



/** @author <no value> */
@Slf4j
@RestController
@RequestMapping("/contract/")
public class ContractController {
  final ContractService contractService;

  public ContractController(ContractService contractService) {
    this.contractService = contractService;
  }

  @RequestMapping("getPage")
  public String getPage(
      Contract contract,
      @RequestParam(name = "page", defaultValue = "1") Integer page,
      @RequestParam(name = "size", defaultValue = "10") Integer size,
      HttpServletRequest request) {
    QueryWrapper<Contract> queryWrapper = QueryGenerator.initQueryWrapper(contract, request.getParameterMap());
    Page<Contract> pageable = new Page<>(page, size);
    IPage<Contract> pageList = contractService.page(pageable, queryWrapper);
    return ResultMsg.success(pageList);
  }

  @RequestMapping(value = "save", method = RequestMethod.POST)
  public String save(@RequestBody Contract contract) {
	  UserDto sysUser = UserInfoUtils.getUserInfo();//SecurityService.getCurrentUserAndCheck(null);
	  contract.setBusinessId(sysUser.getBusinessId().intValue());
	  contract.setStatus(0);
    return ResultMsg.success(contractService.save(contract));
  }

  @RequestMapping(value = "edit", method = RequestMethod.POST)
  public String edit(@RequestBody Contract contract) {
    return ResultMsg.success(contractService.updateById(contract));
  }

  @RequestMapping(value = "get")
  public String get(@RequestParam(name = "id") Long id) {
    return ResultMsg.success(contractService.getById(id));
  }

  @RequestMapping(value = "delete", method = RequestMethod.POST)
  public String delete(@RequestBody Long id) {
    return ResultMsg.success(contractService.removeById(id));
  }
}
